/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBeans;

import Bean.Curso;
import dao.LeiDAO;
import dao.ProfDAO;
import dao.RelatMensalDAO;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import Bean.Escola;
import Bean.Lei;
import Bean.Plano_acao;
import Bean.Professor;
import Bean.RelatMensal;
import java.io.File;
import java.io.FileInputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.context.ExternalContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.primefaces.context.RequestContext;

/**
 *
 * @author alexandre
 */
@ManagedBean
@SessionScoped
public class RelatMensalFaces implements Serializable {

    private RelatMensal selectedRelatMensal;
    private Professor selectedProfessor;
    private Lei selectedLei;
    private Escola selectedEscola;
    private List<Lei> leis;
    private List<Professor> professores;
    private List<RelatMensal> relatorios;
    private List<Plano_acao> planos;
    private Plano_acao selectedPlanoAcao;
    private Curso selectedCurso;
    private String arquivo;
    private List meses;

    public List<RelatMensal> getRelatorios() {
        return relatorios;
    }

    public Plano_acao getSelectedPlanoAcao() {
        return selectedPlanoAcao;
    }

    public void setSelectedPlanoAcao(Plano_acao selectedPlanoAcao) {
        this.selectedPlanoAcao = selectedPlanoAcao;
    }

    public void setRelatorios(List<RelatMensal> relatorios) {
        this.relatorios = relatorios;
    }
    private List<RelatMensalFaces> relatMensais;
    @EJB
    private RelatMensalDAO relatDAO;
    @EJB
    private LeiDAO leiDAO;
    @EJB
    private ProfDAO profDAO;

    public List<Professor> getProfessores() {
        return professores;
    }

    public void setProfessores(List<Professor> professores) {
        this.professores = professores;
    }

    public Curso getSelectedCurso() {
        return selectedCurso;
    }

    public void setSelectedCurso(Curso selectedCurso) {
        this.selectedCurso = selectedCurso;
    }

    public RelatMensal getSelectedRelatMensal() {
        return selectedRelatMensal;
    }

    public void setSelectedRelatMensal(RelatMensal selectedRelatMensal) {
        this.selectedRelatMensal = selectedRelatMensal;
    }

    public List<Plano_acao> getPlanos() {
        return planos;
    }

    public void setPlanos(List<Plano_acao> planos) {
        this.planos = planos;
    }

    public List<RelatMensalFaces> getRelatMensais() {
        return relatMensais;
    }

    public void setRelatMensais(List<RelatMensalFaces> relatMensais) {
        this.relatMensais = relatMensais;
    }

    public Professor getSelectedProfessor() {
        return selectedProfessor;
    }

    public void setSelectedProfessor(Professor selectedProfessor) {
        this.selectedProfessor = selectedProfessor;
    }

    public Lei getSelectedLei() {
        return selectedLei;
    }

    public void setSelectedLei(Lei selectedLei) {
        this.selectedLei = selectedLei;
    }

    public Escola getSelectedEscola() {
        return selectedEscola;
    }

    public void setSelectedEscola(Escola selectedEscola) {
        this.selectedEscola = selectedEscola;
    }

    public List<Lei> getLeis() {
        return leis;
    }

    public void setLeis(List<Lei> leis) {
        this.leis = leis;
    }
    
    public List getMeses() {
        return meses;
    }

    public void setMeses(List meses) {
        this.meses = meses;
    }
    
    

    public String startAddRelatorio() {
        selectedRelatMensal = new RelatMensal();
        selectedEscola = new Escola();
        return "/pages/relatorio.jsf";

    }

    public String startRelatMensal() {
        System.out.println("Relatório Mensal Criado");
        selectedRelatMensal = new RelatMensal();
        selectedEscola = new Escola();
        selectedLei = new Lei();
        selectedProfessor = new Professor();
        selectedCurso = new Curso();

        return "/pages/relatorio_mensal.jsf";

    }

    public String startRelatGeral() {
        System.out.println("Relatório Geral");
        selectedRelatMensal = new RelatMensal();
        return "/admin/relatorioGeralEscolas.jsf";
    }

    public String startRelatPlano() {
        System.out.println("Relatório Plano Ação Criado");
        selectedRelatMensal = new RelatMensal();

        return "/pages/plano_acao.jsf";

    }
    
    public String startRelatAllEscolas(){
        System.out.println("Relatório geral criado");
        selectedRelatMensal = new RelatMensal();
        listaMeses();
        return "/admin/relatorioGeralEscolas.jsf";
    }

    public void add() {
        try {


            selectedRelatMensal.setProfessor(selectedProfessor);
            selectedRelatMensal.setEscola(selectedEscola);
            selectedRelatMensal.setLei(selectedLei);

            relatDAO.add(selectedRelatMensal);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "SIAC", "Dados Gravados com Sucesso!!");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
            selectedRelatMensal = new RelatMensal();
            selectedEscola = new Escola();
            selectedLei = new Lei();
            selectedProfessor = new Professor();
        } catch (Exception e) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "SIAC", "Erro ao Gravar Dados!!");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }

    public void del() {
        relatDAO.del(selectedRelatMensal);
        FacesMessage message = new FacesMessage("Dados Apagados com Sucesso!!");
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
    
    public void edit(){
        relatDAO.edit(selectedRelatMensal);
        FacesMessage message = new FacesMessage("Dados Alterados com Sucesso!!");
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
    
    public String starEdit(){
        return "/admin/Edit_relatorio_mensal.jsf";
    }

    /*public void findLeiPorEscola(){
     leis =  leiDAO.getLeiPorProfessor(selectedProfessor.getID());
     }*/
    public List findleiPorProfessor() {
        this.leis = null;
        this.leis = new ArrayList();
        this.leis = leiDAO.getLeiPorProfessor(selectedProfessor.getID());

        return leis;
    }

    public void findProfessorPorEscola() {
        professores = null;
        professores = profDAO.getRetornaProfPorEscola(selectedEscola.getID());
    }
   


    public void listaMeses() {
        meses = new ArrayList();
        meses.add("Janeiro");
        meses.add("Fevereiro");
        meses.add("Março");
        meses.add("Abril");
        meses.add("Maio");
        meses.add("Junho");
        meses.add("Julho");
        meses.add("Agosto");
        meses.add("Setembro");
        meses.add("Outubro");
        meses.add("Novembro");
        meses.add("Dezembro");

    }

    public void reset() {

        RequestContext.getCurrentInstance().reset("form3:relMensalLEI");
        selectedRelatMensal = new RelatMensal();
    }

    public List<RelatMensal> relatPorEscolaMes() {

        String mes = selectedRelatMensal.getMes();
        int id = selectedEscola.getID();
        this.relatorios = relatDAO.getRelatAllEscola(mes);
        return relatorios;

    }

  public void findAllEscolaPorMes() {
        this.relatorios = null;
        this.relatorios = relatDAO.getRelatAllEscola(selectedRelatMensal.getMes());
       
    }

    public List<Plano_acao> planoPorProfessor() {
        int id = selectedProfessor.getPlanoacao().getId();
        this.planos = relatDAO.getPlanoPorProfessor(id);
        return planos;
    }
    
     public void gerarRelatGeralPDF() throws ClassNotFoundException, SQLException {


        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        ServletContext context = (ServletContext) externalContext.getContext();

        arquivo = context.getRealPath("WEB-INF/relatorios/report2.jasper");
        //this.relatorios = new ArrayList();


        //String mes = selectedRelatMensal.getMes();
        // int id = selectedEscola.getID();
        //this.relatorios = relatDAO.getRel_Escola_mes(mes, id);

        JRDataSource jrRS = new JRBeanCollectionDataSource(relatPorEscolaMes());
        String arquivo1 = FacesContext.getCurrentInstance().getExternalContext().getRealPath("WEB-INF" + File.separator + "imagens" + File.separator + "crede.jpg");

        File crede = new File(arquivo1);
        System.out.println(crede.isFile());

        String arquivo2 = FacesContext.getCurrentInstance().getExternalContext().getRealPath("WEB-INF" + File.separator + "imagens" + File.separator + "seduc.jpg");

        File seduc = new File(arquivo2);
        System.out.println(seduc.isFile());

        Map parameters = new HashMap();
        parameters.put("crede", crede);
        parameters.put("seduc", seduc);





        gerarRelatorioWeb(jrRS, parameters, arquivo);

    }
    public void gerarRelatGeralXLS() throws ClassNotFoundException, SQLException {


        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        ServletContext context = (ServletContext) externalContext.getContext();

        arquivo = context.getRealPath("WEB-INF/relatorios/report2.jasper");
        //this.relatorios = new ArrayList();


        //String mes = selectedRelatMensal.getMes();
        // int id = selectedEscola.getID();
        //this.relatorios = relatDAO.getRel_Escola_mes(mes, id);

        JRDataSource jrRS = new JRBeanCollectionDataSource(relatPorEscolaMes());
        String arquivo1 = FacesContext.getCurrentInstance().getExternalContext().getRealPath("WEB-INF" + File.separator + "imagens" + File.separator + "crede.jpg");

        File crede = new File(arquivo1);
        System.out.println(crede.isFile());

        String arquivo2 = FacesContext.getCurrentInstance().getExternalContext().getRealPath("WEB-INF" + File.separator + "imagens" + File.separator + "seduc.jpg");

        File seduc = new File(arquivo2);
        System.out.println(seduc.isFile());

        Map parameters = new HashMap();
        parameters.put("crede", crede);
        parameters.put("seduc", seduc);





        gerarRelatorioWeb(jrRS, parameters, arquivo);

    }

    public void gerarRelatorioPlanoPDF() throws ClassNotFoundException, SQLException {


        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        ServletContext context = (ServletContext) externalContext.getContext();

        arquivo = context.getRealPath("WEB-INF/relatorios/plano_acao.jasper");
        //this.relatorios = new ArrayList();


        //String mes = selectedRelatMensal.getMes();
        // int id = selectedEscola.getID();
        //this.relatorios = relatDAO.getRel_Escola_mes(mes, id);

        JRDataSource jrRS = new JRBeanCollectionDataSource(planoPorProfessor());
        String arquivo1 = FacesContext.getCurrentInstance().getExternalContext().getRealPath("WEB-INF" + File.separator + "imagens" + File.separator + "crede.jpg");

        File crede = new File(arquivo1);
        System.out.println(crede.isFile());

        String arquivo2 = FacesContext.getCurrentInstance().getExternalContext().getRealPath("WEB-INF" + File.separator + "imagens" + File.separator + "seduc.jpg");

        File seduc = new File(arquivo2);
        System.out.println(seduc.isFile());

        Map parameters = new HashMap();
        parameters.put("crede", crede);
        parameters.put("seduc", seduc);
        
        gerarRelatorioWeb(jrRS, parameters, arquivo);

    }
    
   

    public void gerarRelatorioPDF() throws ClassNotFoundException, SQLException {


        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        ServletContext context = (ServletContext) externalContext.getContext();

        arquivo = context.getRealPath("WEB-INF/relatorios/report2.jasper");
        //this.relatorios = new ArrayList();


        //String mes = selectedRelatMensal.getMes();
        // int id = selectedEscola.getID();
        //this.relatorios = relatDAO.getRel_Escola_mes(mes, id);

        JRDataSource jrRS = new JRBeanCollectionDataSource(relatPorEscolaMes());
        String arquivo1 = FacesContext.getCurrentInstance().getExternalContext().getRealPath("WEB-INF" + File.separator + "imagens" + File.separator + "crede.jpg");

        File crede = new File(arquivo1);
        System.out.println(crede.isFile());

        String arquivo2 = FacesContext.getCurrentInstance().getExternalContext().getRealPath("WEB-INF" + File.separator + "imagens" + File.separator + "seduc.jpg");

        File seduc = new File(arquivo2);
        System.out.println(seduc.isFile());

        Map parameters = new HashMap();
        parameters.put("crede", crede);
        parameters.put("seduc", seduc);





        gerarRelatorioWeb(jrRS, parameters, arquivo);

    }

    private void gerarRelatorioWeb(JRDataSource jrRS, Map<Object, Object> parametros, String arquivo) {
        ServletOutputStream servletOutputStream = null;
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();

        try {
            servletOutputStream = response.getOutputStream();
            JasperRunManager.runReportToPdfStream(new FileInputStream(new File(arquivo)), response.getOutputStream(), parametros, jrRS);

            response.setContentType("application/pdf");
            servletOutputStream.flush();
            servletOutputStream.close();
            context.renderResponse();
            context.responseComplete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
